package com.example.a10.service.impl;

import cn.hutool.core.io.FileUtil;
import com.example.a10.mapper.CosManager;
import com.example.a10.newentity.PictureUrl;
import com.example.a10.service.FileService;
import com.example.a10.util.FileUploadBizEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

@Service
@Slf4j
public class FileServiceImpl implements FileService {

    @Resource
    CosManager cosManager;

    @Override
    public String uploadFile(MultipartFile multipartFile, FileUploadBizEnum fileUploadBizEnum) {
        validFile(multipartFile, fileUploadBizEnum);
        String uuid = RandomStringUtils.randomAlphanumeric(5);
        String filename = uuid + "-" + multipartFile.getOriginalFilename();
        String bizEnumValue = fileUploadBizEnum.getValue();
        String filepath = String.format("%s/%s/%s","file", bizEnumValue, filename);
        File file = null;
        try {
            // 上传文件
            file = File.createTempFile(filepath, null);
            multipartFile.transferTo(file);
            cosManager.putObject(filepath, file);
        } catch (IOException e) {
            log.error("file upload error, filepath = " + filepath, e);
            throw new RuntimeException(e);
        } finally {
            if (file != null) {
                // 删除临时文件
                boolean delete = file.delete();
                if (!delete) {
                    log.error("file delete error, filepath = {}", filepath);
                }
            }
        }
  /*      PictureUrl pictureUrl = new PictureUrl();
        pictureUrl.setUrl(filepath);*/
        //返回链接
        return  filepath;
    }

    @Override
    public void validFile(MultipartFile multipartFile, FileUploadBizEnum fileUploadBizEnum) {
        // 文件大小
        long fileSize = multipartFile.getSize();
        // 文件后缀
        String fileSuffix = FileUtil.getSuffix(multipartFile.getOriginalFilename());
        final long ONE_M = 1024 * 1024L;
        if (FileUploadBizEnum.Train.equals(fileUploadBizEnum)) {
            if (fileSize > ONE_M*100) {
                System.out.println("文件大小不能超过 100M");
            }
            if (!Arrays.asList("txt", "xls", "xlsx", "xlsm", "xlsb","csv").contains(fileSuffix)) {
                System.out.println("文件类型错误");
            }
        }
        if (FileUploadBizEnum.Model.equals(fileUploadBizEnum)||FileUploadBizEnum.Train.equals(fileUploadBizEnum)||
                FileUploadBizEnum.Result.equals(fileUploadBizEnum)){
            if (fileSize>ONE_M*100){
                System.out.println("文件大小不能超过 100M");
            }
            if (!Arrays.asList("txt", "xls", "xlsx", "xlsm", "xlsb","csv").contains(fileSuffix)) {
                System.out.println("文件类型错误");
            }
        }
    }


}
